package com.jeecms.wicool.dao.user.impl;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.user.UserInfoDao;
import com.jeecms.wicool.entity.user.UserInfo;

@Repository
public class UserInfoDaoImpl extends HibernateBaseDao<UserInfo, Integer>
		implements UserInfoDao {

	@Override
	protected Class<UserInfo> getEntityClass() {
		// TODO Auto-generated method stub
		return UserInfo.class;
	}
	@Override
	public Pagination getPage(Integer shopId,String mobile,String name,Short status,Integer carAdviserId,Date q_createTime_start,
			Date q_createTime_end,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from UserInfo bean where 1=1 ");
			
			if (null!=shopId){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",shopId);
			}if(!StringUtils.isBlank(name)){
				f.append(" and bean.name like :name");
				f.setParam("name","%"+ name+"%");
			}if(null!=status){
				f.append(" and bean.status = :status");
				f.setParam("status",status);
			}
			if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			if (null != carAdviserId){
				f.append(" and bean.sceneUser.id = :id");
				f.setParam("id",carAdviserId);
			}
			if(StringUtils.isNotBlank(mobile)){
				f.append(" and bean.mobile like :mobile");
				f.setParam("mobile", Long.valueOf(mobile));
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (null != q_createTime_start){
				String start = dateFormat.format(q_createTime_start)+" 00:00:00";
				f.append(" and bean.createTime >= :start");
				try {
					f.setParam("start", simpleTime.parse(start));
				} catch (ParseException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			if (null != q_createTime_end) {
				String end = dateFormat.format(q_createTime_end)+" 23:59:59";
				f.append(" and bean.createTime <= :end");
				try {
					f.setParam("end", simpleTime.parse(end));
				} catch (ParseException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
			f.append(" order by bean.id desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public UserInfo findById(Integer id) {
		UserInfo entity = get(id);
		return entity;
	}
	@Override
	public UserInfo save(UserInfo bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public UserInfo deleteById(Integer id) {
		UserInfo entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<UserInfo> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from UserInfo bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}